package com.huaao.micropolice.root.security;

import com.huaao.micropolice.core.common.Constants;
import com.huaao.micropolice.core.model.system.User;
import com.huaao.micropolice.core.service.system.UserService;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.PathMatchingFilter;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class SysUserFilter extends PathMatchingFilter {

    @Autowired
    private UserService userService;

    @Override
    protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
        User user = (User)SecurityUtils.getSubject().getPrincipal();
        if(user != null){
        	request.setAttribute(Constants.CURRENT_USER, userService.findByUsername(user.getUsername()));
        }
        return true;
    }
}
